package com.qby.common.utils;


import org.apache.commons.lang.RandomStringUtils;

import java.util.Base64;

/**
 * @describe: base64加密解密工具
 * @author: yang
 * @date: 2020-03-26 10:45
 */
public class Base64Utils {
    /**
     * base64 加密
     *
     * @param password
     * @return
     */
    public static String encryption(String password, String salt) {
        //拼接盐和密码
        String newStr = password + salt;
        byte[] bytes = newStr.getBytes();
        //Base64 加密
        String encoded = Base64.getEncoder().encodeToString(bytes);
        String newEncodedStr = salt + encoded;
        return newEncodedStr;
    }

    /**
     * base64 解密
     *
     * @param password
     * @param salt
     * @return
     */
    public static String decrypt(String password, String salt) {
        //Base64 解密
        String originalStr = password.replaceFirst(salt, "");
        byte[] decoded = Base64.getDecoder().decode(originalStr);
        String decodeStr = new String(decoded);
        String originalPassword = decodeStr.replaceFirst(salt, "");
        return originalPassword;
    }

    /**
     * 生成随机长度盐
     *
     * @param number
     * @return
     */
    public static String generateRandomSalt(int number) {
        String randomSalt = RandomStringUtils.random(number, true, true);
        return randomSalt;
    }

    /**
     * 生成随机字母
     *
     * @param number
     * @return
     */
    public static String generateLetter(int number) {
        String randomLetter = RandomStringUtils.random(number, true, false);
        return randomLetter;
    }
}
